: undefined reference to `pow'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohitsaxena019
    New Member
    • Feb 2010
    • 1

    : undefined reference to `pow'

    Hi all,

    I am playing with pow() function using gcc compiler. I have got some intresting error while playing. Here I am writing the code as well as the problem

    main()
    {
    int i,j,k;
    j=10;
    k=2;
    i=pow(j,k);
    printf("i=%d",i );
    }

    It is the Complete code no math directory included.
    when i compile this using the following command:
    gcc test.c
    It gives the following error
    /var/tmp//ccUP0AVk.o(.tex t+0x3f): In function `main':
    : undefined reference to `pow'


    when i compile this using the following command:
    gcc -lm test.c
    It gives no error and correct output

    Now I had make some changes in the program

    main()
    {
    int i,j,k;
    j=10;
    k=2;
    i=pow(10,2);
    printf("i=%d",i );
    }


    when i compile this using the following command:
    gcc test.c
    It gives no error and correct output

    Q1:-) Is it not necessary to include files in the code.
    Q2:-) Why it is not giving error in the second code.

    Plz help me out.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    A #include file provides information to the compiler that is used to convert one source file into one object file.

    The -lm command-line switch provides information to the linker that is used to combine and convert one or more object files into an executable image file.

    You should #include all relevant header files to insure consistent and repeatable operation of the compiler. In your specific case, the header files provide function prototypes for pow() and printf().

    I don't know why the linker succeeded without -lm in your second example. It is a curiosity; I wouldn't worry about it.

    Comment

    Working...